package com.sgb.businessmodule.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.harisucici.jetpackbase.bus.UIChangeLiveData;
import com.sgb.businessmodule.BusinessConfig;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

public class BusinessSearchModel extends HBaseViewModel {

    //商机历史搜索数据
    public MutableLiveData<List<String>> historySearchList = new MutableLiveData();
    //删除商机历史搜索数据状态
    public MutableLiveData<Boolean> deleteSearchStatus = new MutableLiveData();
    //搜索关键字数据
    public MutableLiveData<List<String>> keywordData = new MutableLiveData();

    @Override
    public UIChangeLiveData getUC() {
        return super.getUC();
    }

    public BusinessSearchModel(@NonNull Application application) {
        super(application);
    }

    /*
     * 获取商机历史搜索数据
     * */
    public void requestHistorySearchData() {
        BusinessConfig.getInstance().getApi().getHistorySearch().enqueue(
                new ICallBack<BaseEntity<List<String>>>() {
                    @Override
                    public void onSuccess(BaseEntity<List<String>> data) {
                        historySearchList.postValue(data.getData());
                    }
                }
        );
    }

    /*
     * 删除商机历史搜索数据
     * */
    public void deleteHistorySearchData() {
        BusinessConfig.getInstance().getApi().deleteHistorySearch().enqueue(
                new ICallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        deleteSearchStatus.postValue(data.getData());
                    }
                }
        );
    }

    /**
     * 存储历史搜索记录
     */
    public void setShardHistorySearch(String content) {
        StringBuilder builder = new StringBuilder();
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, "");
        if (!TextUtils.isEmpty(hsValue)) {
            if (!hsValue.contains(content)) {
                builder.append(hsValue).append("#").append(content);
            } else {
                builder.append(hsValue);
            }
        } else {
            builder.append(content);
        }
        util.saveData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, builder.toString());
    }

    /*
     * 获取本地商机历史搜索数据
     * */
    public List<String> getHistorySearchList() {
        List<String> hsValueList = new ArrayList<>();
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, "");
        if (TextUtils.isEmpty(hsValue)) {
            return null;
        }
        if (hsValue.contains("#")) {
            String[] hsValues = hsValue.split("#");
            for (String value : hsValues) {
                hsValueList.add(value);
            }
        } else {
            hsValueList.add(hsValue);
        }
        return hsValueList;
    }

    /**
     * 删除搜索记录集合
     */
    public boolean clearHistorySearchData() {
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        util.saveData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, "");
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, "");
        if (TextUtils.isEmpty(hsValue))
            return true;
        return false;
    }

    /*
     * 获取商机关键字相关数据
     * */
    public void requestKeywordSearchData(String keyword) {

    }
}
